package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import groovy.swing.impl.DefaultAction

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import java.text.DecimalFormat;
import java.math.BigDecimal;
import java.io.File;

import org.apache.commons.lang.math.NumberUtils;
/**
 * 
 * @author msjykf
 * 生成邮件待发任务
 select 
 ec.batch_id
 ,ec.template_id
 ,ec.sk_invpty
 ,ec.invpty_name
 ,ec.mobile_phone
 ,ec.email
 ,ec.bk_fundaccount
 ,ec.agencyname
 ,ec.open_date
 ,nvl(eb.expectedstartdate,sysdate) as expectedstartdate
 ,dt.filename
 ,dt.c_taskcode
 ,dp.priority
 ,dp.channel
 ,dt.description
 ,eb.createdate
 from email_custinfo ec 
 left join email_send_batch eb on ec.batch_id=eb.batch_id
 left join doctemplate dt on ec.template_id=dt.doctemplate_id
 left join docproperty dp on ec.template_id=dp.doctemplate_id
 where eb.state=1 and eb.procstate=1
 *
 */
public class EmailCreateTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(EmailCreateTask.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		try {
			PropertiesUtil pt = new PropertiesUtil();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			DecimalFormat tf = new DecimalFormat("0.00");
			Calendar cal = Calendar.getInstance();
		
			def currentdate = cal.get(cal.YEAR)+'年'+(cal.get(cal.MONTH)+1)+'月'+cal.get(cal.DATE)+'日';
			def batch_id = map.get("batch_id");
			def template_id = map.get("template_id");
			
			def doctype = map.get("c_taskcode");
			def sk_invpty = map.get("sk_invpty");
			def invpty_name = map.get("invpty_name");
			def email = Util.filterForObject(map.get("email"));
			def bk_fundaccount = Util.filterForObject(map.get("bk_fundaccount"));
			def agencyname = Util.filterForObject(map.get("agencyname"));
			def open_date = Util.filterForObject(map.get("open_date"));
			def createdate = map.get("createdate"); //批次创建时间
			def oldfilename = Util.filterForObject(map.get("emailfailename")); //邮件模板存储文件名称
			def filename = template_id+oldfilename; //邮件模板存储文件名称
			def smap = "";
			def i = 0;
			JSONObject jsonout = new JSONObject();
			jsonout.put("customername",invpty_name);
			jsonout.put("fundaccount",bk_fundaccount);
			jsonout.put("agentcyname",agencyname);
			JSONObject jsonin = new JSONObject();
			JSONArray arr = new JSONArray();
			jsonout.put("currentdate",currentdate);//当前日期
			MapSqlParameterSource params = new MapSqlParameterSource(); //sql防注入方式
			params.addValue("batch_id",batch_id);
			params.addValue("template_id",template_id);
			params.addValue("priority",map.get("priority"));
			params.addValue("pipe",map.get("channel"));
			params.addValue("expectedstartdate",df.format(map.get("expectedstartdate")));
			params.addValue("subject",map.get("description"));
			params.addValue("sk_invpty",sk_invpty);
			params.addValue("invpty_name",invpty_name);
			params.addValue("email",email);
			if(doctype.equals("0001")) {
				def moible = Util.filterForObject(map.get("mobile_phone"));
				def birthdaysmsid = pt.readValue("birthdaysmsid"); //生日祝福短信模板ID
				if(!moible.equals("")) {
					eliteBean.update("insert into sms_send_sync(rec_guid,batch_id,sms_id,sk_invpty,invpty_name,mobile_phone,sms_content,createdate) select sysguid,sb.batch_id,'"+birthdaysmsid+"',"+sk_invpty+",'"+invpty_name+"','"+moible+"',replace(st.content,'[c_custname]','"+invpty_name+"'),sysdate from sms_send_batch sb left join sms_template st on sb.sms_id=st.sms_id where sb.sms_id='"+birthdaysmsid+"' and sb.createdate>trunc(sysdate)","dataSource");
					eliteBean.update("update sms_send_batch set messages=messages+1 where sms_id='"+birthdaysmsid+"' and createdate>trunc(sysdate)","dataSource");
				};
				String[] data=new String[2];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(doctype.equals("0002")) {
				smap = eliteBean.chaxun_sql("select product_cname,bk_product,agencyname,dueshares,due_date from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("bk_product")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c4",tf.format(smap[i].get("dueshares",0)));
					jsonin.put("c5",Util.filterForObject(smap[i].get("due_date")));
					jsonin.put("c6","");
					arr.add(jsonin);
				};
				jsonout.put("table",arr);
				String[] data=new String[4];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[2] = "insert into email_st_maturity_his(rec_guid,batch_id,sk_invpty,bk_product,product_cname,agencyname,dueshares,due_date,createdate) select rec_guid,batch_id,sk_invpty,bk_product,product_cname,agencyname,dueshares,due_date,createdate from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[3] = "delete from email_st_maturity where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(doctype.equals("0003")) {
				smap = eliteBean.chaxun_sql("select reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+" order by reg_date","dataSource");
				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("reg_date")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c4",Util.filterForObject(smap[i].get("bonus_type")));
					jsonin.put("c5",Util.filterForObject(smap[i].get("div_date")));
					jsonin.put("c6",tf.format(smap[i].get("total_shares",0)));
					jsonin.put("c7",tf.format(smap[i].get("capital",0)));
					jsonin.put("c8",Util.filterForObject(smap[i].get("netvalue")));
					jsonin.put("c9",tf.format(smap[i].get("real_shares",0)));
					arr.add(jsonin);
				};
				jsonout.put("table",arr);
				String[] data=new String[4];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[2] = "insert into email_funddiv_confirm_his(rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares,createdate) select rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,capital,netvalue,real_shares,createdate from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[3] = "delete from email_funddiv_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(doctype.equals("0004")) {
				smap = eliteBean.chaxun_sql("select max(date_d) traday from c_calendar where workflag=1 and date_d<=to_date('"+sdf.format(createdate)+"','yyyy-mm-dd')","dataSource");
				def traday = sdf.format(smap[0].get("traday")); //获取最后一个交易日
				jsonout.put("confirm_date",traday);
				smap = eliteBean.chaxun_sql("select req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+" order by req_date","dataSource");
				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("req_date")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("tradetype")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c4",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c5",tf.format(smap[i].get("req_balance",0)));
					jsonin.put("c6",tf.format(smap[i].get("tradefare",0)));
					jsonin.put("c7",Util.filterForObject(smap[i].get("netvalue")));
					jsonin.put("c8",tf.format(smap[i].get("confirm_balance",0)));
					jsonin.put("c9",tf.format(smap[i].get("confirm_shares",0)));
					jsonin.put("c10",Util.filterForObject(smap[i].get("transtatus")));
					jsonin.put("c11",Util.filterForObject(smap[i].get("cause")));
					arr.add(jsonin);
				}
				jsonout.put("table",arr);
				String[] data=new String[4];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[2] = "insert into email_fundtrade_confirm_his(rec_guid,batch_id,sk_invpty,req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause,createdate) select rec_guid,batch_id,sk_invpty,req_date,tradetype,product_cname,agencyname,req_balance,tradefare,netvalue,confirm_balance,confirm_shares,transtatus,cause,createdate from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[3] = "delete from email_fundtrade_confirm where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(doctype.equals("0005")) {
				jsonout.put("opendate",open_date);
				String[] data=new String[2];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			}else if(doctype.equals("0006")) {
			
			def firstDay;
			def lastDay;	
			def sk_kdate;
			def defdate;
			def PI_TPLID = ""; //old邮件模板ID

			def smap1 = eliteBean.chaxun_sql("SELECT PI_TPLID FROM  preferences WHERE DOCTEMPLATE_ID='"+template_id+"'","dataSource");
			if(smap1.size()>0){
			PI_TPLID = Util.filterForObject(smap1[0].get("PI_TPLID"));
			}
			//处理月度对账单入参时间
			if(PI_TPLID.equals("")||PI_TPLID==null){
			//20190312邮件模板内容时间
			sk_kdate = eliteBean.chaxun_sql("select to_char(sk_date,'yyyymmdd')as sk_date,to_char(sk_edate,'yyyymmdd')as sk_edate from preferences where doctemplate_id='"+template_id+"'","dataSource");
			String sk_date=sk_kdate.get(0).get("sk_date");
			String sk_edate=sk_kdate.get(0).get("sk_edate");
			//log.debug("确认开始时间:"+sk_date);
			//log.debug("确认结束时间:"+sk_edate);
				if(sk_date.equals("")||sk_date==null)
				{
					//获取上一个月开始结束时间
					defdate = eliteBean.chaxun_sql("SELECT to_char(add_months(last_day(add_months(sysdate,-1))+1,-1),'yyyymmdd') AS defstartDay,to_char(last_day(add_months(sysdate,-1)),'yyyymmdd') AS defendDay from dual","dataSource");
					firstDay=defdate.get(0).get("defstartDay");
					lastDay=defdate.get(0).get("defendDay");
				}
				else
				{
					firstDay =getFirstDay(sk_date);
					if(sk_edate.equals("")||sk_edate==null)
				   {
					  lastDay=getLastDay(sk_date);
				   }
				   else
				   {
						lastDay=getLastDay(sk_edate);
				   }
				};
			}else{//机构客户阅读对账单
			//20200716邮件模板内容时间
			sk_kdate = eliteBean.chaxun_sql("select to_char(sk_date,'yyyymmdd')as sk_date,to_char(sk_edate,'yyyymmdd')as sk_edate from triggermode where doctemplate_id='"+template_id+"'","dataSource");
			String sk_date=sk_kdate.get(0).get("sk_date");
			String sk_edate=sk_kdate.get(0).get("sk_edate");
			log.debug("确认开始时间1:"+sk_date);
			log.debug("确认结束时间1:"+sk_edate);
			
			//获取前一天时间
			//defdate = eliteBean.chaxun_sql("SELECT to_char(sysdate-1,'yyyymmdd') AS defstartDay from dual","dataSource");
			//firstDay=defdate.get(0).get("defstartDay");
			//lastDay=defdate.get(0).get("defstartDay");
			defdate = eliteBean.chaxun_sql("select max(DATE_I) traday from c_calendar where workflag=1 and DATE_I<=to_char(sysdate,'yyyymmdd')-1","dataSource");
				if(sk_date.equals("")||sk_date==null)
				{//获取最后一个交易日
					firstDay = defdate.get(0).get("traday"); 
					lastDay = defdate.get(0).get("traday");
				}
				else
				{
					firstDay =sk_date;
					if(sk_edate.equals("")||sk_edate==null)
				   {
					lastDay = defdate.get(0).get("traday");
				   }
				   else
				   {
						lastDay=sk_edate;
				   }
				};
			}
				log.debug("确认开始时间2:"+firstDay);
			    log.debug("确认结束时间2:"+lastDay);
				jsonout.put("startdate",firstDay);
				jsonout.put("enddate",lastDay);
				smap = eliteBean.chaxun_sql("select product_cname,agencyname,bonus_type,shares,netvalue,capital,income from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+"","dataSource");
				
				BigDecimal t_capiltal = new BigDecimal("0");
				BigDecimal t_income = new BigDecimal("0");

				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("bonus_type")));
					jsonin.put("c4",tf.format(smap[i].get("shares",0)));
					jsonin.put("c5",Util.filterForObject(smap[i].get("netvalue")));
					jsonin.put("c6",tf.format(smap[i].get("capital",0)));
					jsonin.put("c7",tf.format(smap[i].get("income",0)));
					
					BigDecimal s1 =new BigDecimal(getDouble(tf.format(smap[i].get("capital",0))));
					BigDecimal s2 =new BigDecimal(getDouble(tf.format(smap[i].get("income",0))));
					
					t_capiltal=t_capiltal.add(s1);
					t_income=t_income.add(s2);
					
					//log.debug("-"+i+"-第6列Format原始值"+tf.format(smap[i].get("capital",0)) +" BigDecimal值:"+s1 +" 汇总值:"+t_capiltal);
					//log.debug("-"+i+"-第7列Format原始值"+tf.format(smap[i].get("income",0)) +" BigDecimal值:"+s2 +" 汇总值:"+t_income);
					
					arr.add(jsonin);
				};
				
				jsonin.put("c1","合计");
				jsonin.put("c2","");
				jsonin.put("c3","");
				jsonin.put("c4","");
				jsonin.put("c5","");
				jsonin.put("c6",tf.format(t_capiltal));
				jsonin.put("c7",tf.format(t_income));
				
				arr.add(jsonin);
				jsonout.put("tablea",arr);

				jsonin = new JSONObject();
				arr = new JSONArray();
				smap = eliteBean.chaxun_sql("select req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+" order by req_date","dataSource");
				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("req_date")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("confirm_date")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c4",Util.filterForObject(smap[i].get("bk_product")));
					jsonin.put("c5",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c6",Util.filterForObject(smap[i].get("tradetype")));
					jsonin.put("c7",tf.format(smap[i].get("amount",0)));
					jsonin.put("c8",tf.format(smap[i].get("shares",0)));
					jsonin.put("c9",Util.filterForObject(smap[i].get("netvalue")));
					jsonin.put("c10",tf.format(smap[i].get("tradefare",0)));
					arr.add(jsonin);
				};
				jsonout.put("tableb",arr);
				jsonin = new JSONObject();
				arr = new JSONArray();
				smap = eliteBean.chaxun_sql("select reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,nvl(real_shares,0) as real_shares  from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty="+sk_invpty+" order by reg_date","dataSource");
				for(i=0;i<smap.size();i++) {
					jsonin.put("c1",Util.filterForObject(smap[i].get("reg_date")));
					jsonin.put("c2",Util.filterForObject(smap[i].get("product_cname")));
					jsonin.put("c3",Util.filterForObject(smap[i].get("agencyname")));
					jsonin.put("c4",Util.filterForObject(smap[i].get("bonus_type")));
					jsonin.put("c5",Util.filterForObject(smap[i].get("div_date")));
					jsonin.put("c6",tf.format(smap[i].get("total_shares",0)));
					jsonin.put("c7",tf.format(smap[i].get("total_profit",0)));
					jsonin.put("c8",Util.filterForObject(smap[i].get("netvalue")));
					jsonin.put("c9",tf.format(smap[i].get("real_shares",0)));
					arr.add(jsonin);
				};
				jsonout.put("tablec",arr);
				String[] data=new String[8];
				data[0] = "insert into email_custinfo_his(rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email) select rec_guid,batch_id,template_id,sk_invpty,invpty_name,birthdate,bk_fundaccount,agencyname,open_date,createdate,mobile_phone,email from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[1] = "delete from email_custinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[2] = "insert into email_fundbalanceinfo_his(rec_guid,batch_id,sk_invpty,product_cname,agencyname,req_balance,bonus_type,shares,netvalue,capital,income,createdate) select rec_guid,batch_id,sk_invpty,product_cname,agencyname,req_balance,bonus_type,shares,netvalue,capital,income,createdate from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[3] = "delete from email_fundbalanceinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[4] = "insert into email_fundtradeinfo_his(rec_guid,batch_id,sk_invpty,req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare,createdate) select rec_guid,batch_id,sk_invpty,req_date,confirm_date,product_cname,bk_product,agencyname,tradetype,amount,shares,netvalue,tradefare,createdate from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[5] = "delete from email_fundtradeinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[6] = "insert into email_funddivinfo_his(rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,real_shares,createdate) select rec_guid,batch_id,sk_invpty,reg_date,product_cname,agencyname,bonus_type,div_date,total_shares,total_profit,netvalue,real_shares,createdate from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				data[7] = "delete from email_funddivinfo where batch_id='"+batch_id+"' and sk_invpty ="+sk_invpty+"";
				int[] dataresult = eliteBean.updateBatch(data,"dataSource");
			};
			if(email.equals("")) {
				return;
			};
			
			// 检查获取服务器文件存储路径
			try{
				PropertiesUtil properties = new PropertiesUtil();
				String filePath = properties.readValue("serverpath") + filename;
				log.debug("EmailCreateTask 模板文件路径:" + filePath);
				
				File templateFile = new File(filePath);
				if(true)
				{
					// 获取配置的服务器访问URL
					def ccserverip = properties.readValue("ccserverip");
					
					// 拼接模板文件下载请求URL
					def httpurl = "http://" + ccserverip + "/ngs/fs/get?file=emailtemplate/" + oldfilename + "&d=true";
					log.debug("EmailCreateTask 模板文件下载请求URL:" + httpurl);
					
					// 执行下载操作
					boolean flag = Util.httpDownLoadFile(httpurl, filePath, 10000);
					
					// 打印文件处理下载结果
					if(flag){
						log.debug("EmailCreateTask 获取模板文件成功:" + flag);
					}
					else{
						log.warn("EmailCreateTask 获取模板文件警告！请求flag:" + flag);
					}
					
				
				}
			}catch(Exception e){
				log.error("EmailCreateTask 检查获取服务器文件存储路径Exception:",e);
			}
			
			def content = Util.getMergeContent(JSONObject.fromObject(jsonout).toString(),""+filename+"","UTF-8");
			params.addValue("content",content);
			eliteBean.update_sql_pro("insert into email_send_task(rec_guid,batch_id,template_id,sk_invpty,invpty_name,email,content,contenttype,state,priority,pipe,createdate,expectedstartdate,subject) values(sysguid,:batch_id,:template_id,:sk_invpty,:invpty_name,:email,:content,'html',0,:priority,:pipe,sysdate,to_date(:expectedstartdate,'yyyy-mm-dd hh24:mi:ss'),:subject)",params,"dataSource");
			eliteBean.update("update email_send_batch set quantityvalid=quantityvalid+1 where batch_id='"+batch_id+"'","dataSource");
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		}
	}



	public static String getDouble(String floatVar) {

		if (NumberUtils.isNumber(floatVar)) {
			return floatVar;
		} else {
			return "0.00";
		}

	}
	//取传入时间的第一天
		public static String getFirstDay(String nowDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();

		Date theDate = null;
		if (nowDate == null || nowDate.length() <= 0) {
			theDate = calendar.getTime();
		} else {
			try {
				theDate = df.parse(nowDate);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(theDate);
		gcLast.set(Calendar.DAY_OF_MONTH, 1);
		String day_first = df.format(gcLast.getTime());
		StringBuffer str = new StringBuffer().append(day_first);
		return str.toString();

	}
	//取传入时间的最后一天
	public static String getLastDay(String nowDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		Date theDate = null;

		if (nowDate == null || nowDate.length() <= 0) {
			theDate = calendar.getTime();
		} else {
			try {
				theDate = df.parse(nowDate);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(theDate);
		gcLast.add(Calendar.MONTH, 1);
		gcLast.set(Calendar.DATE, 1);
		gcLast.add(Calendar.DATE, -1);
		String day_last = df.format(gcLast.getTime());
		StringBuffer endStr = new StringBuffer().append(day_last);
		day_last = endStr.toString();
		StringBuffer str = new StringBuffer().append(day_last);
		return str.toString();
	}
}